﻿@{
    ViewBag.Title = "Quản lý tỉnh thành";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutPageAdmin.cshtml";
}

<h2 class="title_h2">Quản lý tỉnh thành - quận huyện</h2>

<div id="grid_object"></div>
<script type="text/x-kendo-template" id="template_object">
     <div class="template_object"></div>
</script>

<script type="text/javascript">
    $(document).ready(function () {
        var crudServiceBaseUrl = "/Admin/Ad_Tinhthanh",
            dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: crudServiceBaseUrl + "/_List",
                        dataType: "json"
                    },
                    create: {
                        url: crudServiceBaseUrl + "/AddTinhThanh",
                        //contentType: "application/json; charset=utf-8",
                        type: "POST",
                        //dataType: "json"
                        complete: function (data, e) {
                            if (data.responseText == "false") {
                                $("#grid_object").data("kendoGrid").dataSource.read();
                                confirmOptionNormal("Có lỗi trong quá trình thêm tỉnh thành");

                            } else {
                                $("#grid_object").data("kendoGrid").dataSource.read();
                            }

                        }

                    },
                    update: {
                        url: crudServiceBaseUrl + "/UpdateTinhThanh",
                        //contentType: "application/json; charset=utf-8",
                        type: "POST",
                        //dataType: "json",
                        complete: function (data, e) {
                            if (data.responseText == "false") {
                                $("#grid_object").data("kendoGrid").dataSource.read();
                                confirmOptionNormal("Có lỗi trong quá trình cập nhật tỉnh thành");

                            } else {
                                $("#grid_object").data("kendoGrid").dataSource.read();
                            }
                        }

                    },
                    destroy: {
                        url: crudServiceBaseUrl + "/DeleteTinhThanh",
                        type: "POST",
                        complete: function (e) {
                            if (data.responseText == "false") {
                                $("#grid_object").data("kendoGrid").dataSource.read();
                                confirmOptionNormal("Có lỗi xả ra trong quá trình xóa đơn vị");

                            } else {
                                $("#grid_object").data("kendoGrid").dataSource.read();
                            }
                        }
                    },
                    parameterMap: function (options, operation) {
                        //var output = null;
                        //if (operation !== "read" && options.models) {
                        //    output = '{ filters: ' + options.models + '}';
                        //}
                        //return kendo.stringify(output);
                        if (operation == "update" && options.models)
                        {
                            return { models: options.models };

                        } else
                        if (operation !== "read" && options.models) {
                            return { models: options.models };
                        }
                    }
                },
                batch: true,
                //pageSize: 20,
                schema: {
                    model: {
                        id: "Id",
                        fields: {
                            Id: { editable: false, nullable: true },
                            Name: { validation: { required: true } },
                            Meta_Title: { validation: { required: true } },
                            Meta_Des: { validation: { required: true } },
                            Hot: { type: "boolean" },
                            stt: { type: "number", validation: { required: true, min: 1} },
                            Icon: { validation: { required: false } },
                            Ma_QG: { validation: { required: false } },
                            Ma_Vung: { validation: { required: false } },
                            Meta_KeyWork: { validation: { required: false } }
                        }
                    }
                }
            });

        $("#grid_object").kendoGrid({
            dataSource: dataSource,
            pageable: true,
            //selectable: "single",
            detailTemplate: kendo.template($("#template_object").html()),
            detailInit: detailInit,
            dataBound: function () {
                this.expandRow(this.tbody.find("tr.k-master-row").first());
            },
            height: $(document).height() - 130,
            scrollable: true,
            //sortable: {
            //    mode: "single",
            //    allowUnsort: false
            //},
            toolbar: ["create"],
            columns: [
                { field: "Name", title: "Tên Tỉnh thành" },
                { field: "Hot", title: "Đặc biệt" },
                { field: "Icon", title: "Icon", hidden : true },
                { field: "Ma_QG", title: "Mã QG" },
                 { field: "Ma_Vung", title: "Mã Vùng" },
                { field: "stt", title: "Số TT" },
                { field: "Meta_Title", title: "Meta Title", hidden: true },
                { field: "Meta_Des", title: "Meta Description", editor: textareaEditor, hidden: true },
                { field: "Meta_KeyWork", title: "Key Work" },
                { command: ["edit", "destroy"], title: "&nbsp;", width: "170px" }],
            editable: { mode: "popup" }
        });
    });

    function textareaEditor(container, options) {
        $('<textarea data-bind="value: ' + options.field + '" cols="30" rows="6"></textarea>')
            .appendTo(container);
    }
    
    function detailInit(e) {

        var detailRow = e.detailRow;

        var crudServiceBaseUrl_menu = "/Admin/Ad_Tinhthanh",

            dataSource_dvnp = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: crudServiceBaseUrl_menu + "/QuanhuyenList?tt=" + e.data.Id,
                        dataType: "json" 
                    },
                    create: {
                        url: crudServiceBaseUrl_menu + "/AddQuanhuyen?tinhthanhid=" + e.data.Id,
                        //contentType: "application/json; charset=utf-8",
                        type: "POST",
                        //dataType: "json"
                        complete: function (data, e) {
                            if (data.responseText == "false") {
                                detailRow.find(".template_object").data("kendoGrid").dataSource.read();
                                confirmOptionNormal("Có lỗi trong quá trình thêm quận huyện");

                            } else {
                                detailRow.find(".template_object").data("kendoGrid").dataSource.read();
                            }

                        }

                    },
                    update: {
                        url: crudServiceBaseUrl_menu + "/UpdateQuanhuyen?tinhthanhid=" + e.data.Id,
                        //contentType: "application/json; charset=utf-8",
                        type: "POST",
                        //dataType: "json",
                        complete: function (data, e) {
                            if (data.responseText == "false") {
                                detailRow.find(".template_object").data("kendoGrid").dataSource.read();
                                confirmOptionNormal("Có lỗi trong quá trình cập nhật quận huyện");

                            } else {
                                detailRow.find(".template_object").data("kendoGrid").dataSource.read();
                            }
                        }

                    },
                    destroy: {
                        url: crudServiceBaseUrl_menu + "/DeleteQuanHuyen",
                        type: "POST",
                        complete: function (e) {
                            if (data.responseText == "false") {
                                detailRow.find(".template_object").data("kendoGrid").dataSource.read();
                                confirmOptionNormal("Có lỗi xả ra trong quá trình xóa quận huyện");

                            } else {
                                detailRow.find(".template_object").data("kendoGrid").dataSource.read();
                            }
                        }
                    },
                    parameterMap: function (options, operation) {
                        //var output = null;
                        //if (operation !== "read" && options.models) {
                        //    output = '{ filters: ' + options.models + '}';
                        //}
                        //return kendo.stringify(output);
                        if (operation !== "read" && options.models) {
                            return { models: options.models };
                        }
                    }
                },
                batch: true,
                pageSize: 20,
                schema: {
                    model: {
                        id: "Id",
                        fields: {
                            Id: { editable: false, nullable: true },
                            TinhthanhId: { editable: false, nullable: true },
                            Name: { validation: { required: true } },
                            Meta_Title: { validation: { required: true } },
                            Meta_Des: { validation: { required: true } },
                            Hot: { type: "boolean" },
                            stt: { type: "number", validation: { required: true, min: 1 } },
                            Icon: { validation: { required: false } },
                            Ma_QG: { validation: { required: false } },
                            Meta_KeyWork: { validation: { required: false } }
                        }
                    }
                }
            });

        detailRow.find(".template_object").kendoGrid({
            dataSource: dataSource_dvnp,
               pageable: true,
               //selectable: "single",
               height: "auto",
               scrollable: false,
               //sortable: {
               //    mode: "single",
               //    allowUnsort: false
               //},
               toolbar: ["create"],
               columns: [
                    { field: "Name", title: "Tên quận huyện" },
                    { field: "Hot", title: "Đặc biệt" },
                    { field: "Icon", title: "Icon", hidden: true },
                    { field: "Ma_QG", title: "Mã Vùng" },
                    { field: "stt", title: "Số TT" },
                    { field: "Meta_Title", title: "Meta Title", hidden: true },
                    { field: "Meta_Des", title: "Meta Description", editor: textareaEditor, hidden: true },
                    { field: "Meta_KeyWork", title: "Key Work" },
                    { command: ["edit", "destroy"], title: "&nbsp;", width: "170px" }],
               editable: "popup"
           });
    }

</script>

